package com.ktg.mes.wm.service.impl;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

import com.ktg.common.core.domain.model.SqlResult;
import com.ktg.common.utils.DateUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ktg.common.utils.SecurityUtils;
import com.ktg.mes.md.domain.MdItem;
import com.ktg.mes.unit.other.SQLListenerInterface;
import com.ktg.mes.unit.other.SQLListenerType;
import com.ktg.mes.wm.domain.WmIssueLine;
import com.ktg.mes.wm.domain.WmItemRecptLine;
import com.ktg.mes.wm.domain.WmWarehouse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ktg.mes.wm.mapper.WmItemSplitLineMapper;
import com.ktg.mes.wm.domain.WmItemSplitLine;
import com.ktg.mes.wm.service.IWmItemSplitLineService;



/**
 * 物料拆分记录Service业务层处理
 *
 * @author byteco
 * @date 2023-01-06
 */
@Service
public class WmItemSplitLineServiceImpl extends ServiceImpl<WmItemSplitLineMapper, WmItemSplitLine> implements IWmItemSplitLineService
{




    /**
     * 查询物料拆分记录
     *
     * @param entity 物料拆分记录
     * @param page 分页对象
     * @return 物料拆分记录
     */
    @Override
    public IPage<WmItemSplitLine> queryPageList(WmItemSplitLine entity, Page<WmItemSplitLine> page) {
        QueryWrapper<WmItemSplitLine> lqw = new QueryWrapper<WmItemSplitLine>(entity);
        lqw.orderByDesc("create_time");
        IPage<WmItemSplitLine> pageList = this.page(page, lqw);
        return pageList;
    }

    /**
     * 查询物料拆分记录
     *
     * @param entity 物料拆分记录
     * @return 物料拆分记录
     */
    @Override
    public List<WmItemSplitLine> queryList(WmItemSplitLine entity) {
        QueryWrapper<WmItemSplitLine> lqw = new QueryWrapper<WmItemSplitLine>(entity);
        lqw.orderByDesc("create_time");
        return this.list(lqw);
    }

    public WmItemSplitLine getInfo(Long id)
    {
        return this.getById(id);
    }


    /**
     * 新增物料拆分记录
     *
     * @param entity 物料拆分记录
     * @return 结果
     */
    @Override
    public void insert(WmItemSplitLine entity)
    {
        entity.setCreateBy(SecurityUtils.getUsername());
        entity.setCreateTime(DateUtils.getNowDate());
        this.save(entity);
    }

    /**
     * 修改物料拆分记录
     *
     * @param entity 物料拆分记录
     * @return 结果
     */
    @Override
    public boolean update(WmItemSplitLine entity)
    {
        entity.setUpdateBy(SecurityUtils.getUsername());
        entity.setUpdateTime(DateUtils.getNowDate());
        return this.updateById(entity);
    }

    /**
     * 批量删除物料拆分记录
     *
     * @param recordIds 需要删除的物料拆分记录主键
     * @return 结果
     */
    @Override
    public boolean delete(Long[] recordIds)
    {

        return this.removeByIds(Arrays.asList(recordIds));
    }

    /**
     * 删除物料拆分记录信息
     *
     * @param recordId 物料拆分记录主键
     * @return 结果
     */
    @Override
    public boolean delete(Long recordId)
    {
        return this.removeById(recordId);
    }

    private LambdaQueryWrapper<WmItemSplitLine> buildQueryWrapper(WmItemSplitLine entity) {
        LambdaQueryWrapper<WmItemSplitLine> lqw = Wrappers.lambdaQuery();
                    lqw.eq(entity.getFromItemRecptLineId() != null, WmItemSplitLine::getFromItemRecptLineId, entity.getFromItemRecptLineId());
                    lqw.eq(entity.getFromAssembleRecptLineId() != null, WmItemSplitLine::getFromAssembleRecptLineId, entity.getFromAssembleRecptLineId());
                    lqw.eq(entity.getItemId() != null, WmItemSplitLine::getItemId, entity.getItemId());
                    lqw.eq(entity.getWarehouseId() != null, WmItemSplitLine::getWarehouseId, entity.getWarehouseId());
                    lqw.eq(entity.getMeterialCode() != null, WmItemSplitLine::getMeterialCode, entity.getMeterialCode());
                    lqw.eq(entity.getBatchCode() != null, WmItemSplitLine::getBatchCode, entity.getBatchCode());
                    lqw.eq(entity.getQuantityRecived() != null, WmItemSplitLine::getQuantityRecived, entity.getQuantityRecived());
                    lqw.eq(entity.getQuantityOut() != null, WmItemSplitLine::getQuantityOut, entity.getQuantityOut());
        return lqw;
    }



}
